#include <bits/stdc++.h>
using namespace std;

int tooct(string str){
    string oct;
    int l = str.size();
    for(int i = 0; i < l; ++i){
        int num = str[i] - '0';
        if(num <= 3) oct += ('0' + num);
        else if(num <= 8) oct += ('0' + num - 1);
        else oct += ('0' + num - 2);
    }
    int res = stoi(oct, nullptr, 8);
    return res;
}

int main(){
    int n;
    cin >> n;
    while(n--){
        string str;
        cin >> str;
        cout << tooct(str) << endl;
    }
}